1 using UnityEngine;
2 using
System.Collections;
3
4 public
class GUIFriendFinding : MonoBehaviour
5 {
6     
private string[] friendListOfSomeCommunity;
7     
public Rect GuiRect;
8
9
10     
void Start()
11     {
12         
// If a user should be "findable", the client must set a playerName before connecting.
13         
// This is then used during connect and the client can be found by others.
14         
// Setting the playerName before connect, enables others to locate your game:
15         PhotonNetwork.playerName =
"usr" + (int)Random.Range(0, 9);
16
17
18         
// Photon Cloud does not implement community features for users but can work with external friends lists.
19         
// We assume you get some list of IDs of your friends.
20         friendListOfSomeCommunity = FetchFriendsFromCommunity();
21
22
23         GuiRect =
new Rect(Screen.width / 4, 80, Screen.width / 2, Screen.height - 100);
24     }
25
26     
27     
// In this demo, wo just make up some names instead of connecting somewhere
28     
public static string[] FetchFriendsFromCommunity()
29     {
30         
string[] friendsList = new string[9];
31         
int u = 0;
32         
for (int i = 0; i < friendsList.Length; i++)
33         {
34             
string usrName = "usr" + u++;
35             
if (usrName.Equals(PhotonNetwork.playerName))
36             {
37                 usrName =
"usr" + u++; // skip friend if the name is yours
38             }
39             friendsList[i] = usrName;
40         }
41
42         
return friendsList;
43     }
44
45
46     
public void OnUpdatedFriendList()
47     {
48         Debug.Log(
"OnUpdatedFriendList is called when the list PhotonNetwork.Friends is refreshed.");
49     }
50
51
52     
public void OnGUI()
53     {
54         
if (!PhotonNetwork.insideLobby)
55         {
56             
// this feature is only available on the Master Client. Check either: insideLobby or
57             
// PhotonNetwork.connectionStateDetailed == PeerState.Authenticated or
58             
// PhotonNetwork.connectionStateDetailed == PeerState.JoinedLobby
59
60             
// for simplicity (and cause we know we WILL join the lobby, we can just check that)
61             
return;
62         }
63
64
65         GUILayout.BeginArea(GuiRect);
66
67         GUILayout.Label(
"Your (random) name: " + PhotonNetwork.playerName);
68         GUILayout.Label(
"Your friends: " + string.Join(", ",this.friendListOfSomeCommunity));
69
70
71         GUILayout.BeginHorizontal();
72         
if (GUILayout.Button("Find Friends"))
73         {
74             PhotonNetwork.FindFriends(
this.friendListOfSomeCommunity);
75         }
76         
if (GUILayout.Button("Create Room"))
77         {
78             PhotonNetwork.CreateRoom(
null); // just a random room
79         }
80         GUILayout.EndHorizontal();
81
82
83         
if (PhotonNetwork.Friends != null)
84         {
85             
foreach (FriendInfo info in PhotonNetwork.Friends)
86             {
87                 GUILayout.BeginHorizontal();
88                 GUILayout.Label(info.ToString());
89                 
if (info.IsInRoom)
90                 {
91                     
if (GUILayout.Button("join"))
92                     {
93                         PhotonNetwork.JoinRoom(info.Room);
94                     }
95                 }
96                 GUILayout.EndHorizontal();
97             }
98         }
99
100         GUILayout.EndArea();
101     }
102 }


If a user should be "findable", the client must set a playerName before connecting.

This is then used during connect and the client can be found by others.

Setting the playerName before connect, enables others to locate your game:

Photon Cloud does not implement community features for users but can work with external friends lists.

We assume you get some list of IDs of your friends.

In this demo, wo just make up some names instead of connecting somewhere

usrName = "usr" + u++; skip friend if the name is yours

this feature is only available on the Master Client. Check either: insideLobby or

PhotonNetwork.connectionStateDetailed == PeerState.Authenticated or

PhotonNetwork.connectionStateDetailed == PeerState.JoinedLobby

for simplicity (and cause we know we WILL join the lobby, we can just check that)

PhotonNetwork.CreateRoom(null); just a random room




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.430 lượt xem

Gõ tìm kiếm nhanh...